﻿// <copyright file="SecdEntry.cs" company="Sinclair Community College">
// Copyright (c) Sinclair Community College. All rights reserved.
// </copyright>

namespace SinclairCC.AppV
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// This class represents a single entry in the security descriptors subsection in an
    /// App-V SFT/DSFT file.
    /// </summary>
    /// <remarks>
    /// The purpose of the security descriptors subsection is to store all the ACL and ACE
    /// entries needed to secure files and directories in the internal directory structure.
    /// </remarks>
    public class SecdEntry : HeaderMetadataSection
    {
        /// <summary>
        /// This field is reserved and is initialized to all zeros.
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "This field is marked as reserved in the App-V File Format Specification.")]
        private byte[] reserved1;

        /// <summary>
        /// The index of this entry in the list of SECD entries.
        /// </summary>
        private short index;

        /// <summary>
        /// This field is reserved and is initialized to all zeros.
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "This field is marked as reserved in the App-V File Format Specification.")]
        private byte[] reserved2;

        /// <summary>
        /// The total length of the Security Descriptor structure, which follows immediately
        /// after this field.
        /// </summary>
        private int size;

        /// <summary>
        /// The security descriptor for this entry.
        /// </summary>
        /// <remarks>
        /// The format in which security descriptors are stored in the SFT file is identical to the format
        /// used by the Windows NT lineage of operating systems to store security descriptors.
        /// </remarks>
        private System.Security.AccessControl.RawSecurityDescriptor rawDescriptor;

        /// <summary>
        /// Initializes a new instance of the SecdEntry class.
        /// </summary>
        /// <param name="fileReader">
        /// A binary file reader that points to the SFT file.
        /// </param>
        public SecdEntry(System.IO.BinaryReader fileReader)
            : base(fileReader)
        {
        }

        /// <summary>
        /// Gets the index of this entry in the list of SECD entries.
        /// </summary>
        public short Index
        {
            get
            {
                this.ReadSection();
                return this.index;
            }
        }

        /// <summary>
        /// Gets the total length of the Security Descriptor structure.
        /// </summary>
        public int Size
        {
            get
            {
                this.ReadSection();
                return this.size;
            }
        }

        /// <summary>
        /// Gets the security descriptor for this entry.
        /// </summary>
        public System.Security.AccessControl.RawSecurityDescriptor RawSecurityDescriptor
        {
            get
            {
                this.ReadSection();
                return this.rawDescriptor;
            }
        }

        /// <summary>
        /// Reads the actual data for this section from the file.
        /// </summary>
        protected override void ReadSection()
        {
            if (this.SectionHasBeenRead)
            {
                return;
            }

            // Seek to the start of the data area for this section.
            long currentPosition = FileReader.BaseStream.Position;
            FileReader.BaseStream.Seek(this.StartPositionInFile + CommonSectionHeader.HeaderSize, System.IO.SeekOrigin.Begin);

            // Read the properties from the file.
            this.reserved1 = FileReader.ReadBytes(8);
            this.index = FileReader.ReadInt16();
            this.reserved2 = FileReader.ReadBytes(8);
            this.size = FileReader.ReadInt32();

            this.rawDescriptor = null;
            if (this.size > 0)
            {
                byte[] securityDescriptorBytes = FileReader.ReadBytes(this.size);
                this.rawDescriptor = new System.Security.AccessControl.RawSecurityDescriptor(securityDescriptorBytes, 0);
            }

            this.SectionHasBeenRead = true;
            FileReader.BaseStream.Seek(currentPosition, System.IO.SeekOrigin.Begin);
        }
    }
}
